class User < ActiveRecord::Base
  has_many :interests
  has_many :tagEvents
  has_many :grades
  has_many :resources
  has_many :comments
  has_many :commentGrades
  
  attr_accessor  :password_confirmation

  
    validates_uniqueness_of :username
    validates_presence_of :username, :password, :email, :name, :on=>:create
    validates_size_of :username, :within => 5..15
    validates_format_of :email,
    :with => /^[A-Za-z0-9](([_.-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([.-]?[a-zA-Z0-9]+)*).([A-Za-z]{2,})$/i
    validates_format_of :username, :with => /^\w+$/i,
    :message => "can only contain letters and numbers."
#   validates_inclusion_of :gender, :in => %w(male female)
#   validates_acceptance_of :agreement
  
    validates_size_of :password, :on => :create, :within => 5..15
    validates_confirmation_of :password, :on => :create
  
  
   def self.md5(password)
     #hash the password after concatenating it with a chosen string, eg: !oslp..
     #to not have a clean md5 hash of the password (more difficult to retrieve pass from md5)
    Digest::MD5.hexdigest("!oslp..#{password}")
  end
  
    def incrementKarma(grade)
    
    self.karma += grade
#     flash[:notice] = "ok"
    self.save
  end
  
   def incrementKarmaFromOtherUser(grade,userId)
   	if (((self.karma / 100) + 1) > 0) then 
  	  grade = ((self.karma / 100) + 1) * grade
    end
    if(self.id != userId) then
    	self.karma += grade
#     flash[:notice] = "ok"
    self.save
    end
  end
  
  def getPrivilegeStatusNumber
  	karmaString = ((self.karma).to_i).to_s
  	return karmaString.length
  end
  
def isMyResource(resource)
	res = Resource.find(:first , :conditions => "id = #{resource.id} AND user_id = #{self.id}")
	return (not res.nil?)
end


   def decodedtext
     keys = {
     "\n"=>"<br />", 
     "[b]"=>"<b>", 
     "[/b]"=>"</b>",
     "[i]"=>"<i>",
     "[/i]"=>"</i>",
     "[u]"=>"<u>",
     "[/u]"=>"</u>",
     "[img]"=>"<img src=\"",
     "[/img]"=>"\">"
     
     }
     text = self.personaltext
     keys.each do |k, v| 
     #puts "\n"+k+" ... "+v+"\n"
      text = text.split(k).join(v)
     end
    return text
  end
  
  	Edit_limit = 1
	Note_watch_limit = 1
	Note_add_HTML_limit = 1
	Tag_add_limit = 1
	Add_remove_resources_limit = 1
	See_others_resources_limit = 1
	Leave_comments_limit = 1
	Html_page_resource_limit = 1
	Html_in_index_limit = 10000
	Html_profile_limit = 1
	Flag_others_limit = 1
	
  def getPrivilegeStatusNumber2
#   	puts ((((self.karma).to_i).to_s).length).to_s + "   *******************"
  	karma = ((self.karma).to_i)
  	return karma
  end
  
    
  def canEditOtherResource
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= Edit_limit)
  end
  
  def canSeeOthersNotes
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= Note_watch_limit)
  end
  
  def canAddHTMLNotesToResource
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= Note_add_HTML_limit)
  end
  
  def canAddTagsToOthersResource
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= Tag_add_limit)
  end
  
  def canAddResourcesToFavourites
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= Add_remove_resources_limit)
  end 
  
  def canSeeOthersResources
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= See_others_resources_limit)
  end 
  
  def canLeaveCommentsToOthersResources
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= Leave_comments_limit)
  end 
  
  def canHaveHTMLPageResource
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= Html_page_resource_limit)
  end 
  
  def canHaveHTMLDivResourceInIndex
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= Html_in_index_limit)
  end 
  
  def canHaveProfileHTMLPage
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= Html_profile_limit)
  end 
  
  def canFlagOtherUser
  	privilege = self.getPrivilegeStatusNumber2
  	return (privilege >= Flag_others_limit)
  end 
  
end
